package task1and3;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class StudentTest extends Student {

    public static void main(String[] args) {
        //1.定义Map集合，用Student对象作为Key，用字符串（此表示学生的住址）作为value
        Map<Student, String> map = new HashMap();
        map.put(new Student("张飞",35), "蜀国");
        map.put(new Student("刘备",45), "蜀国");
        map.put(new Student("关羽",40), "蜀国");
        //2.利用四种遍历方式遍历Map集合中的内容，格式： key::value
        String s1 = map.toString();
        System.out.println(s1);
        System.out.println("------------------------------------------------------");
        //（a)第一种 for each + keySet
        Set<Student> s2 = map.keySet();
        for (Student key: s2) {
            System.out.println(key + "::" + map.get(key));
        }
        System.out.println("------------------------------------------------------");
        //（b)第二种 for each + entrySet
        Set<Map.Entry<Student, String>> s3 = map.entrySet();
        for (Map.Entry<Student, String> stu : s3) {
            System.out.println(stu.getKey() + "::" + stu.getValue());
        }
        System.out.println("------------------------------------------------------");
        //（c)第三种 iterator + entrySet
        Iterator<Map.Entry<Student, String>> it = s3.iterator();
        while(it.hasNext()) {
            Map.Entry<Student, String> stu = it.next();
            System.out.println(stu.getKey() + "::" + stu.getValue());
        }
        System.out.println("------------------------------------------------------");
        //（d)第四种 iterator + keySet
        Iterator<Student> it1 = s2.iterator();
        while(it1.hasNext()) {
            Student key4 = it1.next();
            String value4 = map.get(key4);
            System.out.println(key4 + "::" + value4);
        }
    }
}
